import Vue from 'vue'
import Vuex from 'vuex'
import { Message } from 'element-ui'
import axios from 'axios'
// 引入login
import login from './login'

//挂载Vuex
Vue.use(Vuex)

//创建VueX对象
const store = new Vuex.Store({
    modules: {
        login
    },
    state: {
        //存放的键值对就是所要管理的状态
        // 修改按钮是否显示
        isModify: false,
        Modify: {
            row: '',
            // 负责判断修改的弹窗类型
            userModify: false,
            goodsModify: false,
            warehouseModify: false,
        },
        // 删除按钮是否显示
        isDelete: false,
        Delete: {
            row: '',
            userDelete: false,
            goodsDelete: false,
            warehouseDelete: false,
        },

        // 表头
        tableColumnList: [],
        // 表格数据
        showList: [],
        // 分页数据
        page: {
            pageSize: 0,
            total: 0,
        },
        // 用户信息
        current: {},

        address: ''

    },
    mutations: {
        //es6语法，等同edit:funcion(){...}
        // 编辑表头
        edit_tableColumnList(state, tableColumnList) {
            state.tableColumnList = tableColumnList
        },
        // 编辑表格数据
        edit_showList(state, showList) {
            state.showList = showList
        },
        // 编辑分页数据
        edit_page(state, page) {
            state.page = page
        },
        // 编辑修改按钮是否显示
        edit_isModify(state, isModify) {
            state.isModify = isModify
        },
        // 编辑删除按钮是否显示
        edit_isDelete(state, isDelete) {
            state.isDelete = isDelete
        },
        // 编辑修改按钮弹窗事件
        edit_Modify(state, item) {
            if (window.sessionStorage.getItem("activePath3") == "user_manage") {
                state.Modify.userModify = true
                state.Modify.row = item
            }
            else if (window.sessionStorage.getItem("activePath3") == "goods_manage") {
                state.Modify.goodsModify = true
                state.Modify.row = item
            }
        },
        // 编辑删除按钮事件
        edit_Delete(state, item) {
            if (window.sessionStorage.getItem("activePath3") == "user_manage") {
                state.Delete.userDelete = true
                state.Delete.row = item
            }
            else if (window.sessionStorage.getItem("activePath3") == "goods_manage") {
                state.Delete.goodsDelete = true
                state.Delete.row = item
            }
        },

        edit_address(state, address) {
            state.address = address
        },

        // 成功提示
        success(state, success) {
            Message({
                showClose: true,
                message: success,
                type: 'success',
                center: true,
            });
        },
        // 失败提示
        error(state, error) {
            Message({
                showClose: true,
                message: error,
                type: 'error',
                center: true,
            });
        },
    },
    actions: {
        /**  
         * 提供了Actions来专门进行异步操作，最终提交mutation方法
         * context 上下文(相当于箭头函数中的this)对象
         * payload 挂载参数
         * 将异步操作封装为一个Promise对象
        */
        // 检查是否有管理权限
        async get_current() {
            // 获取token
            axios.interceptors.request.use((config) => {
                config.headers.authentication = window.sessionStorage.getItem("token")
                return config;
            })
            // 调用接口查看当前用户信息
            const { data: res } = await axios.post('/gim/api/user/current')
            if (res.code == 1) {
                this.state.current = res.response
                // this.state.current.a = 1
                return true
            }
        },
    }
})

export default store